package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.FranchiseeBiz;
import com.hst.capacity.application.biz.NotificationBiz;
import com.hst.capacity.application.biz.SmsBiz;
import com.hst.capacity.application.biz.VehicleBiz;
import com.hst.capacity.domain.common.constants.AliTripTransferConstants;
import com.hst.capacity.domain.common.constants.MessageTemplate;
import com.hst.capacity.domain.model.entity.franchisee.customer.CustomerFranchiseeEntity;
import com.hst.capacity.domain.model.request.notification.NotificationParam;
import com.hst.capacity.domain.repository.redis.NotificationRedis;
import com.hst.capacity.infra.external.sms.entinfo.SmsService;
import com.hst.capacity.infra.persistence.jpa.franchisee.customer.CustomerFranchiseeRepository;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class NotificationBizImpl implements NotificationBiz {

    @Autowired
    NotificationRedis notificationRedis;

    @Autowired
    SmsService smsService;

    @Autowired
    SmsBiz smsBiz;

    @Autowired
    VehicleBiz vehicleBiz;

    @Autowired
    FranchiseeBiz franchiseeBiz;

    @Autowired
    CustomerFranchiseeRepository customerFranchiseeRepository;

    @Override
    public int newOrderRemind(NotificationParam param) {
        List<String> idFranchiseeArr = franchiseeBiz.findIdFranByDispacther(param.getIdDispatcher());
        Integer notificationNum = 0;
        if (idFranchiseeArr != null && idFranchiseeArr.size() > 0) {
            for (String idFranchisee : idFranchiseeArr) {
                notificationNum = notificationNum + notificationRedis.getWebNewOrderNotificationValue(idFranchisee);
            }
        } else {
            notificationNum = notificationRedis.getWebNewOrderNotificationValue(param.getIdFranchisee());
        }
        return notificationNum;
    }

    @Override
    public void clearNewOrderRemind(String idDispatcher, String idFranchisee) {
        List<String> idFranchiseeArr = franchiseeBiz.findIdFranByDispacther(idDispatcher);
        if (idFranchiseeArr != null && idFranchiseeArr.size() > 0) {
            for (String id : idFranchiseeArr) {
                notificationRedis.clearWebNewOrderNotification(id);
            }
        } else {
            notificationRedis.clearWebNewOrderNotification(idFranchisee);
        }
    }

    @Override
    public void sendSmsMessage(String mobile, String context) {
//        //发短信
//        String vicheTypeName = vehicleBiz.findVehicleType(1);
//        String flightNo = "CA8528";
//        String message = String.format(MessageTemplate.BizTravelSms.ORDER_DISPATCH_DRIVER, "2023-12-18 00:20", flightNo, vicheTypeName, "17602102982");
//        log.info("message:" + message);
//        smsBiz.sendSmsMessage(mobile, message, AliTripTransferConstants.MemberID.get(AliTripTransferConstants.AppKey.LAOBING));

//        if (StringUtil.isBlank(context))
//            context = "您有新订单啦，用车时间为2023-12-18 00:20,航班号:CA8528，车型：经济型，客户手机：17119486851。";
//        ToOneSMS sms = new ToOneSMS(mobile, context);
//        smsService.sendSMSToSingle(sms, SmsConstants.key.MaDingShangLv);
    }

}
